import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userPhoto: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202107%2F18%2F20210718002444_9d6fd.thumb.1000_0.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1665996329&t=2edebe694f997bfae45ef309ac843092', // 用户头像默认地址
    userName: '', // 用户名字
    exculdeCachedViews: [] // keep-alive动态缓存
  },
  getters: {},
  mutations: {
    // 更改用户头像
    SET_USERPHOTO(state, value) {
      state.userPhoto = value
    },
    // 更改用户名字
    SET_USERNAME(state, value) {
      state.userName = value
    },
    // 增加排除缓存的页面
    ADD_CACHE_VIEW(state, view) {
      state.exculdeCachedViews.push(view)
    },
    // 删除排除缓存的页面
    DELETE_CACHE_VIEW: (state, index) => {
      state.exculdeCachedViews.splice(index, 1)
    }
  },
  actions: {
    // 动态添加排除缓存
    addExculdeCacheView({ commit, state }, view) {
      if (!state.exculdeCachedViews.includes(view)) {
          commit('ADD_CACHE_VIEW', view)
      }
    },
    // 动态删除排除缓存
    deleteExculdeCacheView({ commit, state }, view) {
      const index = state.exculdeCachedViews.indexOf(view)
      if (index > -1) {
       commit('DELETE_CACHE_VIEW', index)
      }
    }

  },
  modules: {}
})
